var dialogId;
var editIndex = undefined;
var operateClassId;	//权限分类ID
var operateClassListStr;	//操作权限集合json串
var operateClassList;		//操作权限集合
var cbbAuthResourceId;		//权限资源信息表ID
var targetType;				//权限设置目标类型
var targetId;				//权限设置目标ID
var authServiceId;			//服务ID
var resCode;				//所选档案分类树节点ID
var resName;				//所选择档案分类树节点名称
var actionType;				//操作类型
var operateAuth;			//操作名称
var relTargetIds;				//已设置的操作权限
var relTargetNames;				//已设置的操作权限名称串
var isBack = false;		//标识是否从返回按钮关闭页面

$(function(){
	if(null == relTargetIds || "null" == relTargetIds){
		relTargetIds = "";
		relTargetNames = "";
	}
	loadTree();
	if(operateClassListStr != null && operateClassListStr != ""){
		operateClassList=eval(operateClassListStr);
	}
});

// 初始化档案分类树
function loadTree() {
	$('#mainTree').tree({
		checkbox : false,
		lines : true,
		dnd : false,
		method : 'post',
		dataType : 'json',
		loadFilter : function(data) {
			if(operateClassId == null || operateClassId == ""){
				return "";
			}
			// 过滤后台返回的数据，处理不同json结构
			if (data.data) {
				return data.data;
			} else {
				return data;
			}
		},
		onClick : function(node) {
			
		},
		formatter:function(node){
			if(node.attributes.IS_OPERATE_BTN == "Y"){
				var c = "";
				if(node.attributes.IS_CONTROL == "Y"){	//节点按钮节点
					c = "<input type='checkbox' value='Y' id='"+node.id+"' checked='checked' disabled='disabled' />"+ '<span title="' + node.text + '">' + node.text + '</span>';
				}
				else{
					c = "<input type='checkbox' value='N' id='"+node.id+"' disabled='disabled' />"+ '<span title="' + node.text + '">' + node.text + '</span>';
				}
				return c;
			}
			return  '<span title="' + node.text + '">' + node.text + '</span>';
		}
	});
}

// 使目标指标信息的必保值和挑战值可以编辑
$.extend($.fn.datagrid.methods, {
	editCell: function(jq,param){
		return jq.each(function(){
			var fields = $("#operateDatagrid").datagrid('getColumnFields',true).concat($(this).datagrid('getColumnFields'));
			for(var i=0; i<fields.length; i++){
				var col = $("#operateDatagrid").datagrid('getColumnOption', fields[i]);
				col.editor1 = col.editor;
				if (fields[i] != param.field){
					col.editor = null;
				}
			}
			$("#operateDatagrid").datagrid('beginEdit', param.index);
			for(var i=0; i<fields.length; i++){
				var col = $("#operateDatagrid").datagrid('getColumnOption', fields[i]);
				col.editor = col.editor1;
			}
		});
	}
});

//双击grid单元格事件
function operateOnDbClickCell(index, field, value){
	/*var row = $('#operateDatagrid').datagrid("getSelected");
	if(row.ID == null || row.ID == ""){
		if (endEditing()){
			if(field == 'OPERATE_CLASS_NAME'){
				$('#operateDatagrid').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
				editIndex = index;
				var ed = $(this).datagrid('getEditor', {index:index,field:field});
				$(ed.target).focus();
			}
		}
	}*/
}

//编辑单元格
function endEditing(){
	if (editIndex == undefined){
		return true;
	}
	if ($('#operateDatagrid').datagrid('validateRow', editIndex)){
		$('#operateDatagrid').datagrid('endEdit', editIndex);
		editIndex = undefined;
		return true;
	} else {
		return false;
	}
}

/**
 * 添加
 */
function addOperateClass(){
	//$('#operateDatagrid').datagrid('endEdit', editIndex);
	var rowCount = $("#operateDatagrid").datagrid("getRows").length;
	if(rowCount == null || rowCount == ""){
		rowCount = -1;
	}
	
	//添加一行记录
	$('#operateDatagrid').datagrid('insertRow',{
		index: rowCount + 1,
		row: {
		}
	});
}

/**
 * 删除操作权限
 */
function delRole(){
	var checkedRows = $("#operateDatagrid").datagrid("getChecked");
	var delTargetIds = "";				//要删除的角色ID串
	var delTargetNames = "";			//要删除的角色名称串
	if(checkedRows.length > 0){
		$.messager.confirm('请确认', '是否确认删除？', function(b) {
			if (b) {
				var delAfterTargetIds = relTargetIds + ",";	//删除后的角色ID串
				var delAfterTargetNames = relTargetNames + ",";	//删除后的角色名称串
				for(var i = 0; i < checkedRows.length; i++){
					delTargetIds += checkedRows[i].ID + ",";
					delTargetNames += checkedRows[i].OPERATE_CLASS_NAME + ",";
					//记录删除后的角色ID，名称串信息，用于删除成功后刷新角色datagrid信息
					delAfterTargetIds = delAfterTargetIds.replace(checkedRows[i].ID + ",", "");
					delAfterTargetNames = delAfterTargetNames.replace(checkedRows[i].OPERATE_CLASS_NAME + ",", "");
				}
				
				$.ajax({
					url : 'platform/UserAuthSetController/delOperateAuthSet',
					data : {
						cbbAuthResourceId : cbbAuthResourceId,
						allTargetIds : relTargetIds,
						delTargetIds : delTargetIds,
						delTargetNames : delTargetNames
						},
					type : 'post',
					dataType : 'json',
					success : function(result) {
						if (result.flag == "0") {
							$("#operateDatagrid").datagrid("options").url = "platform/UserAuthSetController/getSetOperatreClass?relTargetId="+delAfterTargetIds;
							$('#operateDatagrid').datagrid('load');
							
							//记录删除后的ID串和名称串
							relTargetIds = delAfterTargetIds;
							relTargetNames = delAfterTargetNames;
							
							for(var i = 0; i < checkedRows.length; i++){
								var index = $("#operateDatagrid").datagrid("getRowIndex",checkedRows[i]);
								$("#operateDatagrid").datagrid("deleteRow",index);
							}
							
							$.messager.show({
								title : '提示',
								msg : "删除成功！"
							});
						}else{
							$.messager.show({
								title : '提示',
								msg : "删除角色操作失败！"
							});
						}
					}
				});
			}
		});
	}
	else{
		$.messager.alert('提示', '请选择权限！', 'warning');
	}
}

/**
 * 操作权限分类单击时间
 * @param rowIndex
 * @param rowData
 */
function operateOnClickRow(rowIndex, rowData){
	if(rowData.ID == null || rowData.ID == ""){
		operateClassId = rowData.OPERATE_CLASS_NAME;	//未保存记录分类名称实际值为ID
	}
	else{
		operateClassId = rowData.ID;
	}
	//刷新右侧树
	$("#mainTree").tree("options").url = "platform/UserAuthSetController/getMenu?operateClassId="+operateClassId;
	$('#mainTree').tree('reload');
}

/**
 * 将权限咧转化为复选框
 */
function formatClassName(value, rowData, rowIndex) {
	var val = value;
	if(operateClassList != null){
		for(var i = 0; i < operateClassList.length; i++){
			if(value == operateClassList[i].id){
				val = operateClassList[i].text;
				if("" == relTargetIds){
					relTargetIds += value;	//value为ID表示没有保存的记录，这里将ID放入已设置ID传中，用于删除后刷新表格数据不丢失
					relTargetNames += val; 
				}
				else{
					relTargetIds += "," + value;	//value为ID表示没有保存的记录，这里将ID放入已设置ID传中，用于删除后刷新表格数据不丢失
					relTargetNames += "," + val; 
				}
				break;
			}
		}
	}
	return val;
}

/**
 * 保存操作权限设置
 */
function saveOperateAuthSet(){
	$('#operateDatagrid').datagrid('endEdit', editIndex);
	if("" == relTargetIds){
		$.messager.alert('提示', '操作权限分类不能为空！', 'warning');
		return;
	}
	$.ajax({
		url : 'platform/UserAuthSetController/saveOperateAuthSet',
		data : {
			cbbAuthResourceId : cbbAuthResourceId,
			targetType : targetType,
			targetId : targetId,
			relTargetIds : relTargetIds,
			relTargetNames : relTargetNames,
			authServiceId : authServiceId,
			resCode : resCode,
			resName : resName,
			actionType : actionType,
			operateAuth : operateAuth
			},
		type : 'post',
		dataType : 'json',
		success : function(result) {
			if (result.flag == "0") {
				parent.saveOperateAuthSetAfter();
				closeWindow();
			}else{
				$.messager.show({
					title : '提示',
					msg : "保存操作权限失败！"
				});
			}
		}
	});
}

/**
 * 返回
 */
function closeWindow(){
	if (parent != null && parent.$('#' + dialogId) != null) {
		isBack = true;
		parent.operateRoleSetAfter(dialogId);
	}
}

/**
 * 窗口关闭事件，刷新父页面操作角色列表
 */
window.onbeforeunload = function(){
	if(!isBack){
		parent.operateRoleSetAfter();
	}
};

/**
 * 单击grid单元格事件
 * @param index
 * @param field
 * @param value
 */
function operateOnClickCell(index, field, value){
	//空记录可以添加
	if(value == null || value == ""){
		
		operateClassId = "";	//进入编辑器清空行ID
		$("#mainTree").tree("options").url = "platform/UserAuthSetController/getMenu?operateClassId="+operateClassId;
		$('#mainTree').tree('reload');
		
		if (endEditing()){
			if(field == 'OPERATE_CLASS_NAME'){
				$('#operateDatagrid').datagrid('selectRow', index).datagrid('editCell', {index:index,field:field});
				editIndex = index;
				var ed = $(this).datagrid('getEditor', {index:index,field:field});
				$(ed.target).combobox({
					onChange : function(newValue, oldValue){
						operateClassId = newValue;
						//新添加完成操作权限分类后，加载操作权限分类对应的权限
						//刷新右侧树
						$("#mainTree").tree("options").url = "platform/UserAuthSetController/getMenu?operateClassId="+operateClassId;
						$('#mainTree').tree('reload');
					}
				});
				$(ed.target).focus();
			}
		}
	}
}

/**
 * 单击浏览器非控件内容任意位置后，关闭编辑器。保存数据
 */
function bodyOnClick(){
	$('#operateDatagrid').datagrid('endEdit', editIndex);
}